package com.secsys.action;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import com.secsys.dto.PePageDto;
import com.secsys.dto.StoCapitalizationDTO;
import com.secsys.dto.StoFinanceprofitDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.dto.StoStockdailyDTO;
import com.secsys.service.StoCapitalizationService;
import com.secsys.service.StoFinanceProfitService;
import com.secsys.service.StoStockService;
import com.secsys.service.StoStockdailyService;


public class PeAction extends  BaseAction<PePageDto>{

	private StoStockService stoStockService;
	
	private StoStockdailyService  stoStockdailyService;
	
	private StoCapitalizationService stoCapitalizationService;
	
	private StoFinanceProfitService stoFinanceProfitService;
	
	private PePageDto pePageDto;
	
	private String stockCode;
	
	private StoStockDTO stockDto;
	
	private String errorMessage;
	
	
	@Override
	public PePageDto getModel() {
		// TODO Auto-generated method stub
		if(pePageDto==null){
			pePageDto = new PePageDto();
		}
		return pePageDto;
	}
	
	/**
	 * 通过股票代码查找个股的市盈率相关信息
	 * @return
	 */
	
	@SuppressWarnings("static-access")
	public String queryPeByStock(){
		
		if("".equals(stockCode) || stockCode==null){	
			errorMessage = "请输入股票代码!";
			return "input";
		}else if(stockCode.matches("\\d{6}")){//匹配输入的是否为6位的纯数字
		try {
			DecimalFormat dcmFmt = new DecimalFormat("0.00");
			
			StoStockdailyDTO stockdailyDto = null;
			
			StoCapitalizationDTO capitalizationDto = null;
			
			
			stockDto = stoStockService.findByStockCode(stockCode);
			
			List<StoStockdailyDTO>  dailyList = stoStockdailyService.queryStoStockdailyByStockCode(stockCode, 0, 3);//查找出股价列表
			
			List<StoCapitalizationDTO> capitalizationDtoList = stoCapitalizationService.queryStoStockdailyByStockCode(Integer.parseInt(stockCode));//查找出总股本list
			
			List<StoFinanceprofitDTO>  stoFinanceprofitDTOList = stoFinanceProfitService.queryStoFinanceprofitByStockCode(stockCode);//查找出静利润列表
			
			if(dailyList!=null && dailyList.size()>0){
				stockdailyDto = dailyList.get(0);//查找出最新的股价
			}
			if(capitalizationDtoList!=null && capitalizationDtoList.size()>0){
				capitalizationDto = capitalizationDtoList.get(0);//查找出最新的总股本
			}
			if(stoFinanceprofitDTOList!=null && stoFinanceprofitDTOList.size()>0 && stockdailyDto!=null && capitalizationDto!=null){
				Calendar c = Calendar.getInstance();

				for(StoFinanceprofitDTO financeprofitDTO : stoFinanceprofitDTOList){
					c.setTime(financeprofitDTO.getFieldDate());
					if(c.get(Calendar.YEAR)==2011){
						if(c.get(Calendar.MONTH)==2){//如果是第一季度的净利润，则按此方法算出动态市盈率
							double dynamicPe =  stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/(financeprofitDTO.getNetGain()*4);
							pePageDto.setDynamicPe(dcmFmt.format(dynamicPe));
						}else if(c.get(Calendar.MONTH)==5){//如果是第二季度的净利润，则按此方法算出动态市盈率
							double dynamicPe =  stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/(financeprofitDTO.getNetGain()*2);
							pePageDto.setDynamicPe(dcmFmt.format(dynamicPe));
						}else if(c.get(Calendar.MONTH)==8){//如果是第三季度的净利润，则按此方法算出动态市盈率
							double dynamicPe =  stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/(financeprofitDTO.getNetGain()*4/3);
							pePageDto.setDynamicPe(dcmFmt.format(dynamicPe));
						}else{//如果是年末的净利润，则按此方法算出动态市盈率
							double dynamicPe =  (stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/financeprofitDTO.getNetGain());
							pePageDto.setDynamicPe(dcmFmt.format(dynamicPe));
						}
					}
					if(c.get(Calendar.YEAR)==2010){
						double staticPe2010 = stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/financeprofitDTO.getNetGain();
						pePageDto.setStaticPe2010(dcmFmt.format(staticPe2010));//2010年静态市盈率
					}
					if(c.get(Calendar.YEAR)==2009){
						double staticPe2009 = stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/financeprofitDTO.getNetGain();
						pePageDto.setStaticPe2009(dcmFmt.format(staticPe2009));//2009年静态市盈率
					}
					if(c.get(Calendar.YEAR)==2008){
						double staticPe2008 = stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/financeprofitDTO.getNetGain();
						pePageDto.setStaticPe2008(dcmFmt.format(staticPe2008));//2008年静态市盈率						
					}
					if(c.get(Calendar.YEAR)==2007){
						double staticPe2007 = stockdailyDto.getEndPrice()*capitalizationDto.getTotalCaptial()/financeprofitDTO.getNetGain();
						pePageDto.setStaticPe2007(dcmFmt.format(staticPe2007));//2007年静态市盈率						
					}
				}
			}
			return "queryPeByStock";
		} catch (Exception e) {
			errorMessage = "数据库暂时没有此数据,请稍后查看!";
			return "input";
		}
		}else{
			errorMessage = "您输入的是："+ stockCode + "请输入正确的股票代码!";
			return "input";
		}
		
	}




	public String getStockCode() {
		return stockCode;
	}


	public void setStockCode(String stockCode) {
		this.stockCode = stockCode;
	}


	public StoStockDTO getStockDto() {
		return stockDto;
	}


	public void setStockDto(StoStockDTO stockDto) {
		this.stockDto = stockDto;
	}


	public String getErrorMessage() {
		return errorMessage;
	}


	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}




	public StoStockService getStoStockService() {
		return stoStockService;
	}




	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}




	public StoStockdailyService getStoStockdailyService() {
		return stoStockdailyService;
	}




	public void setStoStockdailyService(StoStockdailyService stoStockdailyService) {
		this.stoStockdailyService = stoStockdailyService;
	}




	public StoCapitalizationService getStoCapitalizationService() {
		return stoCapitalizationService;
	}




	public void setStoCapitalizationService(
			StoCapitalizationService stoCapitalizationService) {
		this.stoCapitalizationService = stoCapitalizationService;
	}




	public StoFinanceProfitService getStoFinanceProfitService() {
		return stoFinanceProfitService;
	}




	public void setStoFinanceProfitService(
			StoFinanceProfitService stoFinanceProfitService) {
		this.stoFinanceProfitService = stoFinanceProfitService;
	}




	public PePageDto getPePageDto() {
		return pePageDto;
	}




	public void setPePageDto(PePageDto pePageDto) {
		this.pePageDto = pePageDto;
	}





}
